home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / mpeg_stat-2.2 / block2spec next >
Encoding:
Text File  |  1995-05-10  |  6.0 KB  |  231 lines

  1. #!/usr/sww/bin/perl
  2. ;# 
  3. ;#  block2spec:  Convert "block_files" to "Spec files"
  4. ;# 
  5. ;#  ARGS:   -qs   -mv   -ftype   -ren  -noslice
  6. ;#          -start N   -end N   -o outfile
  7. ;#
  8. ;#  mpeg_stat -block_info block_file foo.mpg
  9. ;# will produce a block information file, which can then be converted
  10. ;# into a "specifics file" for mpeg_encode 1.5.  This is useful if you have
  11. ;# created an mpeg, and would like to recreate it with slightly different 
  12. ;# parameters, for example keeping all the same motion vectors, but with a 
  13. ;# different bit rate....
  14. ;# In that case you would only want the motion vectors information -mv
  15. ;#
  16. ;# a -makerelative option would be nice....
  17. ;# and something to realize all block are the same, and set frame QS etc.
  18. ;#
  19. ;# Output is to stdout, unless -o is used.
  20.  
  21.  
  22. ;($myname = $0) =~ s,.*/,,;
  23. ;$usage = <<_;
  24. Usage: $myname [-qs] [-mv] [-ftype] [-ren] [-start N] [-end N] [-o outfile] block_file
  25. Options:
  26.     -qs    Put in Qscale information
  27.     -mv    Put in motion vector information
  28.     -ftype Put in frame type information (I, P, B)
  29.     -all   All of the above (i.e. make the video the same).
  30.     -noslice   Do not copy slice information
  31.     -start N   (-sN) Begin at frame N
  32.     -end N     (-eN) End at frame N
  33.     -ren   (renumber) If used with -start, makes output frames start at 0
  34.     -o outfile  Output to file outfile
  35. _
  36.  
  37.  
  38. ;# Which parts of the spec to keep:
  39. $start = 0;
  40. $end = -1;
  41. $qs = 0;
  42. $mv = 0;
  43. $ft = 0;
  44. $noslice = 0;
  45. $renumber = 0;
  46. $outfile = "";
  47.  
  48.  
  49. ;# Go through the args....
  50. $i = 0;
  51. $ARGC = $#ARGV;
  52.  
  53. while ($i < $ARGC) {
  54.     $_ = $ARGV[$i];
  55.                 # simple options
  56.     if (/^-mv$/) { $mv = 1; $i++; next;}
  57.     if (/^-qs$/) { $qs = 1; $i++; next;}
  58.     if (/^-ftype$/) { $ft = 1; $i++; next;}
  59.     if (/^-ren$/) { $renumber = 1; $i++; next;}
  60.     if (/^-renumber$/) { $renumber = 1; $i++; next;}
  61.     if (/^-noslice$/) { $noslice = 1; $i++; next;}
  62.     if (/^-all$/) { 
  63.     $mv = 1; $qs = 1; $ft = 1;
  64.     $i++; 
  65.     next;
  66.     }
  67.  
  68.  
  69.                 # two part options
  70.     if (/^-start$/) { $i++; $start=$ARGV[$i]; $i++; next;}
  71.     if (/^-end$/)   { $i++; $end=$ARGV[$i]; $i++; next;}
  72.     if (/^-o$/) {
  73.     $i++;
  74.     $outfile = $ARGV[$i];
  75.     $i++;
  76.     next;
  77.     }
  78.                 # appended options
  79.     ($car, $cdr) = /^-?(.)(.*)/;
  80.     if ($car eq 's') { $start=$cdr; $i++; next;}
  81.     if ($car eq 'e') { $end=$cdr; $i++; next;}
  82.     if ($car eq 'o') {
  83.     $outfile = $cdr;
  84.     $i++;
  85.     next;
  86.     }
  87.     &usage("Unknown option: $_\n\n");
  88.     $i++;
  89. }
  90.  
  91.                 # Setup output
  92. $file_name = $ARGV[$ARGC];
  93. if ($outfile ne "") {
  94.     open(OUTPUT, ">$outfile") || die "$outfile: $!\n";
  95.     select(OUTPUT);
  96. }
  97. print("/* Auto-converted block-info file */\n");
  98.  
  99. $frame = 0;
  100. open(INPUT, $file_name) || die "$file_name: $!\n";
  101. $_ = <INPUT>;            # Get comment line
  102. print $_;            # Copy comment into output
  103. print "version 2\n";
  104. while (<INPUT>) {
  105.     ($car, $cdr) = /^(\S*) (.*)/;
  106.     $_ = $cdr;
  107.     if ($car eq "gop") {
  108.     next;
  109.     }
  110.  
  111.     if ($car eq "frame") {
  112.     ($mfn, $mft, $mpx) = /(\d*) (\S) (\S*)/;
  113.     $frame = $mfn;
  114.     if ($renumber == 1) {    # virtual frame number?
  115.         $vfn = $frame - $start;
  116.     } else {
  117.         $vfn = $frame;
  118.     }
  119.     if ($frame < $start) {next;}
  120.     if (($end >= 0) && ($frame > $end)) {last};
  121.     if ($ft == 0) {
  122.         print "frame $vfn - 0\n";
  123.     } else {
  124.         print "frame $vfn $mft 0\n";
  125.     }
  126.     next;
  127.     }
  128.     if ($frame < $start) {next;}    
  129.     if ($car eq "slice") {
  130.     if ($noslice == 1) {next;}
  131.     ($msn, $msqs) = /(\d*) (\d*)/;
  132.     if ($qs == 0) {
  133.         print "slice $msn 0\n";
  134.     } else {
  135.         print "slice $msn $msqs\n";
  136.     }
  137.     next;
  138.     }
  139.  
  140.     if ($car eq "block") {
  141.     #sample: block 1 B 6 39 forw+back <0, 1> <1, 0> 100010
  142.     @blockinfo = split(' ');
  143.     $bn = $blockinfo[0];
  144.     $bqs = $blockinfo[2];
  145.     if ($qs == 0) {
  146.         $vqs = 0;
  147.     } else {
  148.         $vqs = $bqs;
  149.     }
  150.     if ($mv == 0) {        # easy ones!
  151.         print "block $bn $vqs\n";
  152.     } else {
  153.         $bty = $blockinfo[4];
  154.         if ($bty eq "skip") {
  155.         print "block $bn $vqs skip\n";
  156.         } elsif ($bty eq intra) { # no way to specify!
  157.         print "block $bn $vqs\n";
  158.         } elsif ($bty eq "forw") {
  159.         $ymv = $blockinfo[5];
  160.         $ymv =~ s/<([-\d]*),/$1/;
  161.         $xmv = $blockinfo[6];
  162.         $xmv =~ s/>//;
  163.         print "block $bn $vqs forw $ymv $xmv\n";
  164.         } elsif ($bty eq "back") {
  165.         $ymv = $blockinfo[5];
  166.         $ymv =~ s/<([-\d]*),/$1/;
  167.         $xmv = $blockinfo[6];
  168.         $xmv =~ s/>//;
  169.         print "block $bn $vqs back $ymv $xmv\n";
  170.         } elsif ($bty eq "forw+back") {
  171.         $fymv = $blockinfo[5];
  172.         $fymv =~ s/<([-\d]*),/$1/;
  173.         $fxmv = $blockinfo[6];
  174.         $fxmv =~ s/>//;
  175.         $bymv = $blockinfo[7];
  176.         $bymv =~ s/<([-\d]*),/$1/;
  177.         $bxmv = $blockinfo[8];
  178.         $bxmv =~ s/>//;
  179.         print "block $bn $vqs bi $fymv $fxmv $bymv $bxmv\n";
  180.         } elsif ($bty eq "0") {
  181.         print "block $bn $vqs\n"; # no way to specify
  182.         } else {
  183.         print STDERR "Block what? $_\n";
  184.         }
  185.     }               
  186.     next;
  187.     }
  188.  
  189.     print STDERR "What is this?: +$car+ -$cdr-\n"
  190. }
  191.  
  192.                 # All done
  193. if ($outfile ne "") {
  194.     close(OUTPUT);
  195. }
  196. exit;
  197.  
  198.  
  199. sub usage {
  200.     select(STDERR);
  201.     print @_, $usage;
  202.     print "$rcsid\n" if $rcsid =~ /:/;
  203.     exit;
  204. }
  205.  
  206.  
  207. # Copyright (c) 1995 University of California at Berkeley
  208. # Written by Steve Smoot
  209. # My first "real" Perl program, so I apologize for any sillyness
  210. #
  211. # Permission to use, copy, modify, and distribute this software and its
  212. # documentation for any purpose, without fee, and without written agreement is
  213. # hereby granted, provided that the above copyright notices and the following
  214. # two paragraphs appear in all copies of this software.
  215. # IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  216. # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  217. # OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  218. # CALIFORNIA or the Technical University of Berlin HAS BEEN ADVISED OF THE 
  219. # POSSIBILITY OF SUCH DAMAGE.
  220. # THE UNIVERSITY OF CALIFORNIA
  221. # SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  222. # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  223. # PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE 
  224. # UNIVERSITY OF CALIFORNIA HAS NO 
  225. # OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 
  226. # OR MODIFICATIONS.
  227. #
  228.